import config
import loger
import reportTotext
import sqlite3
import pathlib

class basecheck:
    def __init__(self,db,rp,log):
        self.db=db
        self.rp=rp
        self.log=log
        self.teacherList=[]
        self.cur=db.cursor()
        self.checkMorning() #早晨语数英至少一节
        self.checkEveryDay()#每个老师每天都有课
        self.EveningCourseCheck()#星期一到星期四最多三段晚自习
        self.checkCourseNum() #检查config中设定科目是否超课时
    def checkMorning(self):
        self.log.debug("开始检查每班语数英早晨至少一节课")
        AllClassName=[]
        for i in config.grade:
            AllClassName=AllClassName+config.grade[i]
        wt=""
        for i in config.MorningSession:
            wt=wt+f"{i},"
        wt=wt[:-1]
        self.log.debug(wt)
        for course_ in ["语文","数学","英语"]:
            for cn in AllClassName:
                sql="select real_course,count(real_course),week as c from main where real_course='{}' and class ='{}' and times in ({})  group by week;".format(course_,cn,wt)
                r=self.cur.execute(sql)
                r=r.fetchall()
                self.log.debug(len(r))
                if(len(r)<5):
                    s=["1","2","3","4","5"]
                    for ss_ in r:
                        s.remove(str(ss_[2]))
                    self.log.debug(s)
                    ss_=""
                    for i in s:
                        ss_=ss_+f"{i},"
                    ss_=ss_[:-1]
                    self.rp.e(f"{cn}星期{ss_}无{course_}课******")
                else:
                    self.rp.i(f"{cn}5天早晨有{course_}课")
    def checkEveryDay(self): #检查是否每天都有课""
        self.rp.i("\n\n开始检查老师是否每天都有课")
        r=self.cur.execute("select name from main group by name;")
        r=r.fetchall()
        for i in r:
            self.teacherList.append(i[0])

        for t in self.teacherList:
            r=self.cur.execute("select week,count(course) from main where name='{}' and week>0 group by week;".format(t))
            r=r.fetchall()
            if(len(r)<5):
                r1=self.cur.execute("select count(course) from main where name='{}';".format(t))
                r1=r1.fetchone()
                self.rp.e("{} \t老师\t共{}节课，\t{}天有课".format(t,r1[0],len(r)))
        self.rp.i("老师是完成否每天都有课检查")

    def checkCourseNum(self):
        self.rp.i("\n\n开始检查各科每天最多课时")
        for i in config.AtMostClass:
            for n in i["class"]:
                for k in i["course"]:
                    self.log.debug(f"正在检查{n}{k[0]}每天课时")
                    sql=f'SELECT Count(real_course) AS num, week FROM main WHERE main.class = "{n}" AND main.real_course = "{k[0]}" GROUP BY week ;'
                    r=self.cur.execute(sql)
                    r=r.fetchall()
                    for j in r:
                        if(j[0]>k[1]):
                            self.rp.e(f"{n}星期{j[1]}有{j[0]}节{k[0]}课")
                        else:
                            self.rp.i(f"{n}星期{j[1]}有{j[0]}节{k[0]}课")

                
        self.rp.i("\n\n完成检查各科每天最多课时")
        


    def EveningCourseCheck(self):
        self.rp.i("\n\n开始检查每个老师的晚自习段数")
        r=self.cur.execute("select name from main group by name;")
        r=r.fetchall()
        for t in r:
            self.teacherList.append(t[0])
            count_=0
            for w in (1,2,3,4):
                r1=self.cur.execute("select * from main where name='{}' and times>{} and week={};".format(t[0],config.AfternoonSession[-1],w))
                r1=r1.fetchall()
               
                if(len(r1)>0):count_=count_+1
                
            if(count_>=4):
                self.rp.e("{}老师有{}段晚自习".format(t[0],count_))
            else:
                self.rp.i("{}老师有{}段晚自习".format(t[0],count_))
        self.rp.i("完成每个老师的晚自习段数检查")

if __name__ == "__main__":
    log = loger.Logger('timetable',StreamLevel='debug',FileLevel='info',fmt='%(asctime)s-%(levelname)s:\t%(message)s').logger
    root=pathlib.Path(__file__).parent
    db=sqlite3.connect(str(root/"classbook.db"))
    cur = db.cursor()
    r=cur.execute("select v from setting where k = 'filepath';")
    r=r.fetchone()
    filepath=r[0]
    rp=reportTotext.reportTotext(filepath,log)
    basecheck(db,rp,log)
    rp.save()
